package com.lab.component.common;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.View;

import com.cuitrip.app.util.FontManager;
import com.lab.app.BaseActivity;
import com.lab.jumper.BundleCenter;
import com.lab.logtrack.AppLog;

/**
 * Created by ak<br><br>
 * DialogFragment基类
 */
public abstract class BaseDialogFragment extends DialogFragment implements IUIController {

    // 当宿主处于Stop状态,是否自动隐藏Dialog
    private boolean mHideWhenStop = true;
    // 图片加载器
//    private ImageLoader mImageLoader = new ImageLoader();
    // Dialog KeyEventCallback
    private DialogKeyEventCallback mDialogKeyEventCallback;
    // Dialog DialogInterface
    private DialogInterface mDialogInterface;

    public boolean isHideWhenStop() {
        return mHideWhenStop;
    }

    public void setHideWhenStop(boolean hideWhenStop) {
        mHideWhenStop = hideWhenStop;
    }

    public boolean isShowing() {
        Dialog dialog = getDialog();
        if (null != dialog) {
            return dialog.isShowing();
        }
        return false;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                if (null != mDialogKeyEventCallback)
                    return mDialogKeyEventCallback.onKeyEvent(keyEvent);
                return false;
            }
        });
        return dialog;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        FontManager.changeRootViewFonts(view);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 释放原始Bundle
        BundleCenter.getInstance().removeBundle(getArguments());
        // 清除ImageLoader
//        clearImageLoader();
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        if (null != mDialogInterface)
            mDialogInterface.cancel();
        super.onCancel(dialog);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if (null != mDialogInterface)
            mDialogInterface.dismiss();
        hideDialogFragment(this);
        super.onDismiss(dialog);
    }

    @Override
    public void setCancelable(boolean cancelable) {
        super.setCancelable(cancelable);
    }

    @Override
    public void setArguments(Bundle args) {
        // 对原始Bundle进行包装
//        Bundle argsWarpper = BundleCenter.getInstance().wrapBundle(args);
        super.setArguments(args);
    }

    @Override
    public final FragmentManager getOptimizedFragmentManager() {
        return this.getChildFragmentManager();
    }

    @Override
    public void showDialogFragment(BaseDialogFragment dialog) {
        if (null != dialog) {
            try {
                ((BaseActivity) getActivity()).showDialogFragment(dialog);
            } catch (Exception e) {
                AppLog.e(e.getMessage());
            }
        }
    }

    @Override
    public void hideDialogFragment(BaseDialogFragment dialog) {
        if (null != dialog) {
            try {
                ((BaseActivity) getActivity()).hideDialogFragment(dialog);
            } catch (Exception e) {
                AppLog.e(e.getMessage());
            }
        }
    }

    @Override
    public void dismiss() {
        hideDialogFragment(this);
    }


    /**
     * 返回操作，处理嵌套
     *
     * @return
     */
    public boolean onBackPressed() {
        FragmentManager childFragmentManager = getChildFragmentManager();
        //TODO remove pop up fragment
//        if (SuperFragmentUtil.tryToPopUpFragment(childFragmentManager)) {
//            return true;
//        } else {
        return directBackPressed();
//        }
    }

    /**
     * 返回操作，不处理嵌套
     *
     * @return
     */
    public boolean directBackPressed() {
        dismiss();
        return true;
    }

    public void setDialogKeyEventCallback(DialogKeyEventCallback dialogKeyEventCallback) {
        mDialogKeyEventCallback = dialogKeyEventCallback;
    }

    public void setDialogInterface(DialogInterface dialogInterface) {
        mDialogInterface = dialogInterface;
    }

    public interface DialogKeyEventCallback {
        /**
         * Dialog KeyEvent回调
         *
         * @param keyEvent
         * @return
         */
        boolean onKeyEvent(KeyEvent keyEvent);
    }

}
